package com.xiaoshuidi.cloud.module.rooms.repository;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomequipment.vo.RoomEquipmentPageReqVO;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomequipment.vo.RoomEquipmentPageResp;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomequipment.vo.RoomEquipmentQueryPd;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomequipment.vo.RoomEquipmentQueryResp;
import com.xiaoshuidi.cloud.module.rooms.enums.EquipmentOwn;
import com.xiaoshuidi.cloud.module.rooms.enums.EquipmentPosition;
import com.xiaoshuidi.cloud.module.rooms.mapper.RoomEquipmentMapper;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomEquipment;
import com.xiaoshuidi.cloud.module.rooms.repository.irepository.IRoomEquipmentRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author Feng
 * @date 2023/4/27
 */

@Component
@RequiredArgsConstructor
public class RoomEquipmentRepository extends ServiceImpl<RoomEquipmentMapper, RoomEquipment> implements IRoomEquipmentRepository {

    private final RoomEquipmentMapper roomEquipmentMapper;

    @Override
    public Long countByCode(String code) {
        LambdaQueryWrapper<RoomEquipment> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomEquipment::getCode, code);
        return this.count(wrapper);
    }

    @Override
    public IPage<RoomEquipmentPageResp> pageRoomEquipmentByRoomId(RoomEquipmentPageReqVO reqVO) {

        IPage<RoomEquipmentPageResp> roomEquipmentPageRespIPage = roomEquipmentMapper.pageRoomEquipmentByRoomId(new Page<RoomEquipment>(reqVO.getPageNo(), reqVO.getPageSize()), reqVO);
        List<RoomEquipmentPageResp> records = roomEquipmentPageRespIPage.getRecords();
        if (CollectionUtil.isNotEmpty(records)) {
            records.forEach(s -> {
                s.setOwnName(EquipmentOwn.getNameByCode(s.getOwn()));
            });
        }
        return roomEquipmentPageRespIPage;
    }

    @Override
    public List<RoomEquipment> exportRoomEquipment(LambdaQueryWrapper<RoomEquipment> wrapper) {
        return roomEquipmentMapper.selectList(wrapper);
    }

    @Override
    public RoomEquipmentQueryResp queryRoomEquipmentById(Integer id) {
        RoomEquipmentQueryResp resp = roomEquipmentMapper.queryRoomEquipmentById(id);
        if (resp.getOwn() != null) {
            resp.setOwnName(EquipmentOwn.getNameByCode(resp.getCode()));
        }
        if (resp.getPosition() != null) {
            resp.setPositionName(EquipmentPosition.getNameByCode(resp.getPosition()));
        }

        return resp;
    }

    @Override
    public List<RoomEquipmentQueryResp> getByRoomId(Long roomHouseId) {
        return roomEquipmentMapper.getByRoomId(roomHouseId);
    }

    @Override
    public List<RoomEquipmentQueryPd> queryPdList(List<Long> apartmentIds) {

        List<RoomEquipmentQueryPd> list = roomEquipmentMapper.queryPdList(apartmentIds);
        return list;
    }

    @Override
    public Boolean unbindingEquipment(Long roomId) {
        roomEquipmentMapper.unbindingEquipment(roomId);
        return true;
    }

    @Override
    public List<RoomEquipment> getByRoomIds(List<Long> roomHouseIds) {
        LambdaQueryWrapper<RoomEquipment> wrapper = Wrappers.lambdaQuery();
        wrapper.in(ObjectUtil.isNotEmpty(roomHouseIds), RoomEquipment::getRoomId, roomHouseIds);
        return list(wrapper);
    }

    public Long countByCategory(Long categoryId) {
        LambdaQueryWrapper<RoomEquipment> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomEquipment::getCategoryId, categoryId);
        return count(wrapper);
    }

    public Long countBySupplier(Long supplierId) {
        LambdaQueryWrapper<RoomEquipment> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomEquipment::getSupplierId, supplierId);
        return count(wrapper);
    }
}
